/*
 * Copyright (C) 2018 wxyz <hyhjwzx@126.com>
 * This program can be distributed under the terms of the GNU GPL Version 2.
 * See the file LICENSE.
 *
 */

package ren.wxyz.study.euler.hp01;

import ren.wxyz.study.euler.base.IProblem;
import ren.wxyz.study.euler.util.ConsoleHelper;

import java.math.BigInteger;

/**
 * 第二题
 *
 * @auther wxyz 2018-02-28_19:48
 * @since 1.0
 */
public class P002 implements IProblem {

    @Override
    public void main(String[] args) throws Throwable {
        long maxNumber = Long.parseLong(args[0]);
        v1(maxNumber);
    }

    /**
     * ok
     *
     * @param limitNumber 限制数
     */
    void v1(long limitNumber) {
        long sum = 2;

        long a = 1;
        long b = 2;
        long c = 0;
        while (c <= limitNumber) {
            c = a + b;

            if (c % 2 == 0) {
                sum += c;
            }

            a = b;
            b = c;
        }

        ConsoleHelper.print("c = " + c);
        ConsoleHelper.print("sum = " + sum);
    }

    /**
     * 超时了，而且对题目理解错误
     *
     * @param limitNumber 限制数
     */
    void v0(long limitNumber) {
        BigInteger sum = BigInteger.valueOf(2);

        // 菲波那切数列
        BigInteger ba = BigInteger.valueOf(1);
        BigInteger bb = BigInteger.valueOf(2);
        BigInteger curr = BigInteger.valueOf(0);


        for (int i = 2; i < limitNumber; i++) {
            curr = ba.add(bb);
            if (curr.remainder(BigInteger.valueOf(2)).equals(BigInteger.ZERO)) {
                sum = sum.add(curr);
            }

            ba = bb;
            bb = curr;
        }

        ConsoleHelper.print("curr = " + curr.toString());
        ConsoleHelper.print("sum = " + sum.toString());
    }
}
